package cn.itcast.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.constant.Constant;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;

@Controller
@Scope("prototype")
@Namespace("/")
@ParentPackage("json-default")
public class OrderAction extends BaseAction<Order>{
	

	private String sendAreaInfo; //发件人信息
	private String recAreaInfo;	//收件人信息
	
	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	//保存订单的功能
	@Action(value="order_save", results={@Result(name="success",location="index.html",type="redirect")})
	public String saveOrder(){
		//保存发货人,收件 人区域信息
		String[] sendStr = sendAreaInfo.split("/");
		Area sendArea = new Area();
		sendArea.setProvince(sendStr[0]);
		sendArea.setCity(sendStr[1]);
		sendArea.setDistrict(sendStr[2]);
		model.setSendArea(sendArea);
		
		Area recArea = new Area();
		String[] recStr = recAreaInfo.split("/");
		recArea.setProvince(sendStr[0]);
		recArea.setCity(sendStr[1]);
		recArea.setDistrict(sendStr[2]);
		model.setRecArea(recArea);
	
		//保存客户的id
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		model.setCustomer_id(21);
			
		//调用bos后台系统service保存订单,传入order对象
		WebClient.create(Constant.BOS_MANAGEMENT_URL+"/services/orderService/order").
			type(MediaType.APPLICATION_JSON).post(model);
		return SUCCESS;
	
	}
}
